import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import ConfigElementType 1.0
import "qrc:/qmlComponents/ConfigManager"
import "qrc:/qmlComponents/AxisModulePos"
import "qrc:/qmlComponents/VisionViewer"
import "qrc:/qmlComponents/CustomizedUIElement/RowColumnSelectorView"
import "qrc:/qmlComponents/CustomizedUIElement"

Item {
    property var dhConfigModel: null
    property var dispenseHead: null
    property bool isRightDH: false
    property string dispZModuleName: ""
    property string dispXyzModuleName: ""
    property string rightDispXyzModuleName: ""

    Component{
        id: measureTrayLevelCom

        Item {
            ColumnLayout{
                XYZModulePos{
                    moduleName: dispXyzModuleName
                    posName: "MeasureTrayLevelLeftTop"
                }
                XYZModulePos{
                    moduleName: dispXyzModuleName
                    posName: "MeasureTrayLevelRightBottom"
                }
                Button{
                    text: qsTr("测量Tray盘水平")
                    onClicked: {
                        tem.runSingleCmd(dispenseHead, "measureTrayLevel")
                    }
                }
            }
        }
    }

    Component{
        id: sensorPrCom

        Item {
            ColumnLayout{
                VisionLocationViewer{
                    calibrationName: "TrayRawSensorCalibration"
                    visionLocationName: "TrayRawSensorLocation"
                    performOriginPrResultVisible: true
                }
                XYZModulePos{
                    moduleName: rightDispXyzModuleName
                    posName: "PrLeftTop"
                }
            }
        }
    }

    Component{
        id: nozzelToLaserOffsetCom

        Item{
            ColumnLayout{
                XYZModulePos{
                    moduleName: dispXyzModuleName
                    posName: "CalibrateNozzelToLaserPos"
                }
                ConfigRow{
                    configName: "measureHeightVelocityRatio"
                    configModel: dispCommonConfig
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                ConfigRow{
                    configName: "measureHeightLimitPos"
                    configModel: dhConfigModel
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                ConfigRow{
                    configName: "laserHeightWhileNozeelTouchedTable"
                    configModel: dhConfigModel
                    Component.onCompleted: {
                        init(ConfigElementType.Double)
                    }
                }
                Button{
                    text: qsTr("CalibrateNozzelToLaserOffset")
                    onClicked: {
                        tem.runSingleCmd(dispenseHead, "calibrateNozzelToLaserTipOffset")
                    }
                }
            }
        }
    }

    Component{
        id: laserToCameraOffsetCom

        Item {
            RowLayout{
                spacing: 20
                ColumnLayout{
                    Layout.alignment: Qt.AlignTop
                    XYZModulePos{
                        moduleName: dispXyzModuleName
                        posName: "LaserToCameraOffsetLaserPos"
                    }
                    XYZModulePos{
                        moduleName: rightDispXyzModuleName
                        posName: "LaserToCameraOffsetPrPos"
                    }
                    VisionLocationViewer{
                        visionLocationName: "LaserToCameraOffsetCaliLocation"
                        calibrationName: "LaserToCameraOffsetCalibration"
                    }
                }

                ColumnLayout{
                    Layout.alignment: Qt.AlignTop
                    ObjectEditor{
                        configModel: laserToCameraOffsetCaliParam
                        sectionName: "laserToCameraOffsetCaliParam"
                        Component.onCompleted: {
                            init()
                        }
                    }
                    ConfigRow{
                        configName: "laserToCameraOffsetX"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    ConfigRow{
                        configName: "laserToCameraOffsetY"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    Button{
                        text: qsTr("CalibrateLaserToCameraOffset")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "calibrateLaserToCameraTipOffset")
                        }
                    }
                }
            }
        }
    }

    Component{
        id: nozzelToCameraOffsetCom

        Item {
            RowLayout{
                spacing: 20
                ColumnLayout{
                    Layout.alignment: Qt.AlignTop
                    XYZModulePos{
                        moduleName: dispXyzModuleName
                        posName: "ShotGlueDotPos"
                    }
                    XYZModulePos{
                        moduleName: rightDispXyzModuleName
                        posName: "GlueDotPrPos"
                    }
                    VisionLocationViewer{
                        visionLocationName: "GlueDotLocation"
                        calibrationName: "GlueDotCalibration"
                    }
                }
                ColumnLayout{
                    Layout.alignment: Qt.AlignTop
                    ConfigRow{
                        configName: "shotGlueDotTime"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Int)
                        }
                    }
                    ConfigRow{
                        configName: "shotGlueZOffset"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    Button{
                        text: "MoveToShotGlueDot"
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "moveToShotGlueDot")
                        }
                    }
                    ConfigRow{
                        configName: "nozeelToCameraOffsetX"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    ConfigRow{
                        configName: "nozeelToCameraOffsetY"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    Button{
                        text: "CalculateNozzelToCameraOffset"
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "calculateNozzelToCameraOffset")
                        }
                    }
                    Button{
                        text: qsTr("CalibrateNozzelToCameraOffset")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "calibrateNozzelToCameraTipOffset")
                        }
                    }
                }
            }
        }
    }

    Component{
        id: dispensPathCom

        Item {
            RowLayout{
                spacing: 20

                ObjectEditor{
                    Layout.alignment: Qt.AlignTop
                    sectionName: "DispenserConfig"
                    configModel: dispenserConfig
                    listMaxHeight: 120
                    Component.onCompleted: {
                        init()
                    }
                }

                ObjectEditor{
                    Layout.alignment: Qt.AlignTop
                    sectionName: "GlueCheckParam"
                    configModel: glueCheckParam
                    Component.onCompleted: {
                        init()
                    }
                }

                ColumnLayout{
                    Layout.alignment: Qt.AlignTop

                    ConfigRow{
                        configName: "initAngle"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    ConfigRow{
                        configName: "readyForDispenseZOffset"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    ConfigRow{
                        configName: "shotGlueZOffset"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    ConfigRow{
                        configName: "dispensePathFileName"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Other)
                        }
                    }
                    ColumnLayoutRowColumnSelector{
                        rowColInfo: dwhRowColInfo
                    }
                    RowLayout{
                        Button{
                            text: qsTr("MoveTo")
                            onClicked: {
                                tem.runSingleCmd(dispenseHead, "moveToPrPos", [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column])
                            }
                        }
                        Button{
                            text: qsTr("SaveImage")
                            onClicked: {
                                tem.runSingleCmd(dispenseHead, "saveImageForEditDispPath")
                            }
                        }
                        Button{
                            text: qsTr("加载点胶路径")
                            onClicked: {
                                dispensePathResultImage.updateImage()
                                tem.runSingleCmd(dispenseHead, "readDispensePath")
                            }
                        }
                    }
                    RowLayout{
                        CheckBox{
                            id: ckbCheckGlue
                            text: qsTr("CheckGlue")
                        }
                        Button{
                            text: qsTr("画胶")
                            onClicked: {
                                tem.runSingleCmd(dispenseHead, "moveToShotGlue", [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column, ckbCheckGlue.checked])
                            }
                        }
                    }

                    Item {
                        width: 200
                        height: 200
                        Image {
                            id: dispensePathResultImage
                            anchors.fill: parent
                            fillMode: Image.PreserveAspectFit

                            function updateImage(){
                                var resultImage = dispenseHead.getDispensePathResultImageName()
                                source = ""
                                if(resultImage === ""){
                                    dispensePathResultImage.source = "qrc:/commonicons/no-image.png"
                                }else{
                                    dispensePathResultImage.source = resultImage
                                }
                            }

                            Component.onCompleted: {
                                updateImage()
                            }
                        }
                    }
                }
            }
        }

    }

    Component{
        id: flyDispensingCalibrationCom

        Item {
            RowLayout{
                spacing: 40

                ObjectEditor{
                    Layout.alignment: Qt.AlignTop
                    configModel: flyDispensingConfig
                    sectionName: "FlyDispensingConfig"
                    Component.onCompleted: {
                        init()
                    }
                }

                ColumnLayout{
                    Layout.alignment: Qt.AlignTop
                    XYZModulePos{
                        moduleName: dispXyzModuleName
                        posName: "ShotGlueDotPos"
                    }
                    Button{
                        text: qsTr("CalibrateShotGlueDotDelay")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "calibrateShotGlueDotDelay")
                        }
                    }
                    RowLayout{
                        Label{
                            text: qsTr("LeftGlueDotCenterX")
                        }
                        TextField{
                            id: txtX1
                            text: "0"
                            DoubleValidator{
                                notation: DoubleValidator.StandardNotation
                            }
                            selectByMouse: true
                            implicitWidth: 50
                        }
                        Button{
                            text: qsTr("Read")
                            onClicked: {
                                txtX1.text = dispenseHead.getCurrentRightDispXPos()
                            }
                        }
                    }
                    RowLayout{
                        Label{
                            text: qsTr("RightGlueDotCenterX")
                        }
                        TextField{
                            id: txtX2
                            text: "0"
                            DoubleValidator{
                                notation: DoubleValidator.StandardNotation
                            }
                            selectByMouse: true
                            implicitWidth: 50
                        }
                        Button{
                            text: qsTr("Read")
                            onClicked: {
                                txtX2.text = dispenseHead.getCurrentRightDispXPos()
                            }
                        }
                    }
                    Button{
                        text: qsTr("CalculateShotGlueDotDelay")
                        onClicked: {
                            dispenseHead.calculateShotGlueDotDelay(txtX1.text, txtX2.text)
                        }
                    }

                    GroupBox{
                        title: qsTr("DispenseShapeBaseOnCurrentPos")
                        ColumnLayout{
                            RowLayout{
                                Label{
                                    text: qsTr("LineLen:")
                                }
                                MyDoubleInput{
                                    id: txtLineLen
                                    text: "30"
                                }
                            }
                            Button{
                                text: qsTr("ShotGlueLine")
                                onClicked: {
                                    tem.runSingleCmd(dispenseHead, "shotGlueLine", [txtLineLen.text])
                                }
                            }
                            RowLayout{
                                Label{
                                    text: qsTr("RectWidth:")
                                }
                                MyDoubleInput{
                                    id: txtRectWidth
                                    text: "4"
                                }
                            }
                            RowLayout{
                                Label{
                                    text: qsTr("RectHeight:")
                                }
                                MyDoubleInput{
                                    id: txtRectHeight
                                    text: "3"
                                }
                            }
                            Button{
                                text: qsTr("ShotRectangle")
                                onClicked: {
                                    tem.runSingleCmd(dispenseHead, "shotRectangle", [txtRectWidth.text, txtRectHeight.text])
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Component{
        id: operationCom

        Item{
            ColumnLayout{
                RowLayout{
                    XYZModulePos{
                        moduleName: dispXyzModuleName
                        posName: "StandbyPos"
                    }
                    SAxisModulePos{
                        moduleName: dispZModuleName
                        posName: "MeasureHeightPos"
                    }
                }

                RowLayout{
                    ConfigRow{
                        configName: "disableLeftDispHead"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Bool)
                        }
                    }
                    ConfigRow{
                        configName: "disableRightDispHead"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Bool)
                        }
                    }
                }

                ConfigRow{
                    configName: "executeGlueCheck"
                    configModel: dispCommonConfig
                    Component.onCompleted: {
                        autoInit()
                    }
                }
                ConfigRow{
                    configName: "measureHeightFre"
                    configModel: dispCommonConfig
                    Component.onCompleted: {
                        init(ConfigElementType.Other)
                    }
                }
                RowLayout{
                    ConfigRow{
                        configName: "shotGlueTimes"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Int)
                        }
                    }
                    Label{
                        text: qsTr("胶水使用时间(h):")
                    }
                    TextField{
                        id: txtGlueUsedTime
                        implicitWidth: 80
                        function updateGlueUsedTime(){
                            text = dispenseHead.glueUsedTime()
                        }
                        Timer{
                            interval: 60000
                            repeat: true
                            running: true
                            triggeredOnStart: true
                            onTriggered: {
                                txtGlueUsedTime.updateGlueUsedTime()
                            }
                        }
                    }
                    Button{
                        text: qsTr("Reset")
                        onClicked: {
                            dispenseHead.newGlueSetUp()
                            txtGlueUsedTime.updateGlueUsedTime()
                        }
                    }
                }

                RowLayout{
                    ConfigRow{
                        configName: "rightDH2LeftDHSafetyRange"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    Button{
                        text: qsTr("Read")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "readRightDH2LeftDHSafetyRange")
                        }
                    }
                }
                RowColumnSelector{
                    rowColInfo: dwhRowColInfo
                }
                RowLayout{
                    ConfigRow{
                        configName: "measureHeightXShift"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            autoInit()
                        }
                    }
                    ConfigRow{
                        configName: "measureHeightPosYShift"
                        configModel: dispCommonConfig
                        Component.onCompleted: {
                            autoInit()
                        }
                    }
                }

                RowLayout{
                    ConfigRow{
                        configName: "shotGlueZHeight"
                        configModel: dhConfigModel
                        Component.onCompleted: {
                            init(ConfigElementType.Double)
                        }
                    }
                    Button{
                        text: qsTr("CalibrateShotGlueHeight")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "calibrateShotGlueHeight",
                                             [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column])
                        }
                    }
                }
                RowLayout{
                    Button{
                        text: qsTr("MoveToPrPos")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "moveToPrPos",
                                            [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column])
                        }
                    }
                    Button{
                        text: qsTr("MoveToMeasureHeight")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "moveToMeasureHeight",
                                             [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column])
                        }
                    }
                }
                RowLayout{
                    CheckBox{
                        id: opCkbCheckGlue
                        text: qsTr("CheckGlue")
                    }
                    Button{
                        text: qsTr("MoveToShotGlue")
                        onClicked: {
                            tem.runSingleCmd(dispenseHead, "moveToShotGlue",
                                             [dwhRowColInfo.unitRow, dwhRowColInfo.unitColumn, dwhRowColInfo.row, dwhRowColInfo.column, opCkbCheckGlue.checked])
                        }
                    }
                }
                Button{
                    text: qsTr("整盘Tray喷胶")
                    onClicked: {
                        tem.runSingleCmd(rightDispHead, "performAllSensorShotGlue")
                    }
                }
            }
        }
    }

    MyTabView{
        indent: 3
        width: parent.width
        height: parent.height

        Component.onCompleted: {
            addTab("Operation", operationCom)
            if(isRightDH){
                addTab("SensorPr", sensorPrCom)
                addTab("TrayLevel", measureTrayLevelCom, "MeasureTrayLevel")
            }
            addTab("NozzelToLaser", nozzelToLaserOffsetCom, "NozzelToLaserOffset")
            addTab("LaserToCamera", laserToCameraOffsetCom, "LaserToCameraOffset")
            addTab("NozzelToCamera", nozzelToCameraOffsetCom, "NozzelToCameraOffset")
            if(isRightDH){
                addTab("DispensePath", dispensPathCom)
            }
            addTab("FlyDispCalibration", flyDispensingCalibrationCom)
        }
    }
}

